<?php
class Shop_Model_Ponta extends Shop_Model_Base{

	const PONTA_WEB_CALLING_NO = '26';

	/**
	 * 表示用変換
	 * @param $tenpoList
	 *
	 * @return 変換後リスト
	 */
	public function convertList($tenpoList)
	{
		$dispData = array();
		
		foreach ($tenpoList as $tenpo) {
			$data = array();
			
			// エリア
			$areaCd = $tenpo['M01_AREA_CD'];
			$areaNm = $tenpo['M01_AREA_NAME'];
			if (!isset($dispData[$areaCd])) {
				$dispData[$areaCd] = array();
				$dispData[$areaCd]['AREA_NM'] = $areaNm;
			}
			// 都道府県
			$data['TODOUFUKEN_CD'] = $tenpo['M05_TENPO_TODOUFUKEN_CD'];
			$data['TODOUFUKEN_NM'] = $tenpo['M05_TENPO_TODOUFUKEN'];
			// 企業名
			$data['HOUZIN_CD'] = $tenpo['M05_HOUZIN_CD'];
			$data['HOUZINMEI'] = Apm_Util::replaceUnicode($tenpo['M05_HOUZINMEI']);
			// 店舗名
			$data['TENPO_CD']   = $tenpo['M05_TENPO_CD'];
			$data['TENPO_NAME'] = Apm_Util::replaceUnicode($tenpo['M05_TENPO_NAME']);
			// 住所
			$data['TENPO_YUUBIN'] = substr($tenpo['M05_TENPO_YUUBIN'], 0, 3) . '-' . substr($tenpo['M05_TENPO_YUUBIN'], 3, 4);
			$data['JYUUSHO_TEXT'] = Apm_Util::replaceUnicode($tenpo['M05_JYUUSHO_TEXT']);
			$data['TENPO_BANCHI'] = Apm_Util::replaceUnicode($tenpo['M05_TENPO_BANCHI']);
			$data['TENPO_JYUUSHO_BIKOU'] = Apm_Util::replaceUnicode($tenpo['M05_TENPO_JYUUSHO_BIKOU']);
			// 電話番号（通常）
			if (!empty($tenpo['M05_TENPO_TEL01'])) {
				$data['TENPO_TEL'] = $tenpo['M05_TENPO_TEL01'];
			} else if (!empty($tenpo['M05_TENPO_TEL02'])) {
				$data['TENPO_TEL'] = $tenpo['M05_TENPO_TEL02'];
			} else {
				$data['TENPO_TEL'] = '-';
			}
			// 無料電話番号チェック
			$callData = Apm_ShopUtil::getCallingData($tenpo);
			if ($callData['IS_FREE_CALL']) {
				if ($tenpo['PONTA_CONTROL_FLAG'] == '0' && !empty($tenpo['PONTA_CONNECTION_NO'])) {
					$data['CONNECTION_NO'] = $tenpo['PONTA_CONNECTION_NO'];
				} else if ($tenpo['M93_CONTROL_FLAG6'] == '0' && !empty($tenpo['M93_CONNECTION_NO6'])) {
					$data['CONNECTION_NO'] = $tenpo['M93_CONNECTION_NO6'];
				} else {
					$data['CONNECTION_NO'] = '-';
				}
			} else {
				$data['CONNECTION_NO'] = '-';
			}
			// リンク先URL
			$data['SHOP_DTL_URL'] = Apm_Bukkenurl::generateUrlByParametersShopDetail($tenpo);
			
			$dispData[$areaCd]['TENPO'][] = $data;
		}
		
		return $dispData;
	}

	/**
	 * ponta店舗一覧所得sql生成
	 *
	 * @param $params
	 * @param $db_params
	 * @return sql文
	 */
	protected function generateSql($params, &$db_params)
	{
		$sql  = "";
		$sql .= "SELECT";
		$sql .= "   M05.M05_TENPO_CD";
		$sql .= " , M05.M05_HOUZIN_CD";
		$sql .= " , M05.M05_TENPO_NAME";
		$sql .= " , M05.M05_HOUZINMEI";
		$sql .= " , M01.M01_AREA_CD";
		$sql .= " , M01.M01_AREA_NAME";
		$sql .= " , M05.M05_TENPO_TODOUFUKEN_CD";
		$sql .= " , M05.M05_TENPO_TODOUFUKEN";
		$sql .= " , M05.M05_TENPO_YUUBIN";
		$sql .= " , M05.M05_JYUUSHO_TEXT";
		$sql .= " , M05.M05_TENPO_BANCHI";
		$sql .= " , M05.M05_TENPO_JYUUSHO_BIKOU";
		$sql .= " , M05.M05_TENPO_TEL01";
		$sql .= " , M05.M05_TENPO_TEL02";
		$sql .= " , M93.M93_USE_KBN";
		$sql .= " , M93.M93_START_DATE";
		$sql .= " , M93.M93_DEL_DATE";
		$sql .= " , M93.M93_CONTROL_FLAG6";
		$sql .= " , M93.M93_CONNECTION_NO6";
		$sql .= " , M93.M93_CONTROL_FLAG".self::PONTA_WEB_CALLING_NO." as PONTA_CONTROL_FLAG";
		$sql .= " , M93.M93_CONNECTION_NO".self::PONTA_WEB_CALLING_NO." as PONTA_CONNECTION_NO";
		$sql .= "  FROM";
		$sql .= "   M05TENPO_INFO M05";
		$sql .= " , M93CALLNOTE M93";		// 2013/08 WEBCALLING→CALLNOTE 切替
		$sql .= " , M01AREA M01";
		$sql .= " WHERE";
		$sql .= "     M05.M05_TENPO_CD = M93.M93_TENPO_CD(+)";
		$sql .= " AND M05.M05_TENPO_TODOUFUKEN_CD = M01.M01_TODOUFUKEN_CD";
		$sql .= " AND M05.M05_KOUKAI_FLAG = 1";
		$sql .= " AND ( M05.M05_TENPO_CD > '01000000'";
		$sql .= "   AND M05.M05_TENPO_CD < '48000000' )";
		$sql .= $this->generateNotinDummyShop("M05.M05_TENPO_CD", $db_params);
		$sql .= " AND M05.M05_PONTA_USE_FLG = 1";				// ponta利用
		$sql .= " AND M93.M93_USE_KBN(+) = '1'";
		$sql .= " AND M93.M93_START_DATE(+) <= sysdate";
		$sql .= " AND nvl(M93.M93_DEL_DATE(+), sysdate+1) > sysdate";
		$sql .= " ORDER BY";
		$sql .= "   M01.M01_AREA_CD ASC";
		$sql .= " , M05.M05_TENPO_TODOUFUKEN_CD ASC";
		$sql .= " , M05.M05_TENPO_SHIKUCHOUSON_CD ASC";
		$sql .= " , M05.M05_TENPO_CD ASC";
		
		return $sql;
	}
}
?>
